package cn.wolfcode.p2p.website.controller;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.ISystemDictionaryService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.base.util.UserContext;
import cn.wolfcode.p2p.business.domain.RechargeOffline;
import cn.wolfcode.p2p.business.domain.UserBankinfo;
import cn.wolfcode.p2p.business.service.IMoneyWithdrawService;
import cn.wolfcode.p2p.business.service.IPlatformBankInfoService;
import cn.wolfcode.p2p.business.service.IRechargeOfflineService;
import cn.wolfcode.p2p.business.service.IUserBankinfoService;
import cn.wolfcode.p2p.website.util.RequireLogin;

/**
 * Created by wolfcode on 2018/7/23.
 */
@Controller
public class PersonalController {
	@Autowired
	private IAccountService accountService;

	@Autowired
	private IUserInfoService userinfoService;

	@Autowired
	private ISystemDictionaryService dicService;

	@Autowired
	private IPlatformBankInfoService bankInfoService;

	@Autowired
	private IRechargeOfflineService rechargeService;

	@Autowired
	private IUserBankinfoService userBankinfoService;
	
	@Autowired
	private IMoneyWithdrawService withdrawService;

	@RequestMapping("/personal")
	@RequireLogin
	public String personalPage(Model model) {
		model.addAttribute("account", accountService.get(UserContext.getCurrent().getId()));
		return "personal";
	}

	/**
	 * 进入个人资料页面
	 */
	@RequestMapping("basicInfo")
	@RequireLogin
	public String basicInfoPage(Model model) {
		model.addAttribute("userinfo", this.userinfoService.get(UserContext.getCurrent().getId()));
		// 根据数据字典编码查询数据字典明细
		model.addAttribute("educationBackgrounds", this.dicService.listBySn("educationBackground"));
		model.addAttribute("incomeGrades", this.dicService.listBySn("incomeGrade"));
		model.addAttribute("marriages", this.dicService.listBySn("marriage"));
		model.addAttribute("kidCounts", this.dicService.listBySn("kidCount"));
		model.addAttribute("houseConditions", this.dicService.listBySn("houseCondition"));
		return "userInfo";
	}

	/**
	 * 保存/修改个人资料
	 */
	@RequestMapping("basicInfo_save")
	@ResponseBody
	public JSONResult saveBasicInfo(UserInfo to) {
		JSONResult result = new JSONResult();
		try {
			this.userinfoService.saveBasicInfo(to);
		} catch (DisplayableException ex) {
			result = new JSONResult(false, ex.getMessage());
		} catch (Exception e) {
			// 把错误信息记录到服务器中日志文件中,方便排查问题.
			result = new JSONResult(false, "系统异常,请联系管理员!");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 线下充值申请页面
	 */
	@RequestMapping("recharge")
	public String rechargePage(Model model) {
		model.addAttribute("banks", this.bankInfoService.list());
		return "recharge";
	}

	/**
	 * 线下充值申请
	 */
	@RequestMapping("recharge_save")
	@ResponseBody
	public JSONResult rechargeApply(RechargeOffline to) {
		JSONResult result = new JSONResult();
		try {
			this.rechargeService.apply(to);
		} catch (DisplayableException ex) { 
			result = new JSONResult(false, ex.getMessage());
		} catch (Exception e) {
			// 把错误信息记录到服务器中日志文件中,方便排查问题.
			result = new JSONResult(false, "系统异常,请联系管理员!");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 到绑定银行卡页面
	 */
	@RequestMapping("bankInfo")
	public String bindBankPage(Model model) {
		UserInfo current = this.userinfoService.get(UserContext.getCurrent().getId());
		if (current.getHasBindBankInfo()) {
			// 判定当前用户是否绑定银行卡
			// 如果绑定--->bankInfo_result.ftl
			model.addAttribute("bankInfo", this.userBankinfoService.getByUserId(current.getId()));
			return "bankInfo_result";
		} else {
			// 否则-->bankInfo.ftl
			model.addAttribute("userinfo", current);
			return "bankInfo";
		}
	}

	/**
	 * 绑定银行卡
	 */
	@RequestMapping("bankInfo_save")
	@ResponseBody
	public JSONResult bindBankinfo(UserBankinfo bi) {
		JSONResult result = new JSONResult();
		try {
			this.userBankinfoService.bind(bi);
		} catch (DisplayableException ex) {
			result = new JSONResult(false, ex.getMessage());
		} catch (Exception e) {
			// 把错误信息记录到服务器中日志文件中,方便排查问题.
			result = new JSONResult(false, "系统异常,请联系管理员!");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 提现申请界面
	 */
	@RequestMapping("moneyWithdraw")
	public String withdrawPage(Model model) {
		UserInfo current = this.userinfoService.get(UserContext.getCurrent().getId());
		model.addAttribute("account", this.accountService.get(current.getId()));
		// 没有绑定银行卡
		// 在审核流程中
		if (current.getHasWithdrawInProcess()) {
			model.addAttribute("haveProcessing", true);
		}
		// 正常申请
		model.addAttribute("bankInfo", this.userBankinfoService.getByUserId(current.getId()));
		return "moneyWithdraw_apply";
	}
	
	/**
	 * 提交提现申请
	 */
	@RequestMapping("moneyWithdraw_apply")
	@ResponseBody
	public JSONResult withDrawApply(BigDecimal moneyAmount){
		JSONResult result = new JSONResult();
		try {
			this.withdrawService.apply(moneyAmount);
		} catch (DisplayableException ex) {
			result = new JSONResult(false, ex.getMessage());
		} catch (Exception e) {
			// 把错误信息记录到服务器中日志文件中,方便排查问题.
			result = new JSONResult(false, "系统异常,请联系管理员!");
			e.printStackTrace();
		}
		return result;
	}
}
